* IV QUANTILE REGRESSION OF EFFECT OF EXPECTED INFLATION 12 MONTHS AHEAD ON:
*		OWN PRICE ANNUAL CHANGE - EXPECTED AT TIME OF INTERVIEW, OBSERVED AT T+1, T+2, T+4
*		LOG LABOR DEMAND  - OBSERVED AT T+1, T+2, T+3, T+4

* USE STATA IMPLEMENTATION (IVQREGRESS SMOOTH) OF KAPLAN-SUN SMOOTHED IVQ REGRESSION WITH DEFAULT CHOICES  

* FOR EACH DEP VAR AND EACH QUARTER I ESTIMATE IVQREG AT 9 DECILES
* FOR EACH DEP VAR I THUS OBTAIN (42QTR X 9 DECILES) COEFFICIENT ESTIMATES
* (LESS THAN 42 FOR FORWARD LOOKING VARS, IE EFFECT ON LAB DEMAND 4 QTRS AHEAD CAN BE ESTIMATED ONLY FOR 42-4 QUARTERS)
* EACH SET OF COEFFICENT ESTIMATES AND ASSOCIATED SE IS STORED IN MATRIX SQTEX_Z, X=P(rices), L(abor Dem) and Z=B(coeff),S(se)
* MATRIX THEN TRANSFORMED IN DTA AND USED TO PRODUCE FIG 8 MAIN TEXT AND TABS H1-H4

* CODE BELOW RUNS IN SEQUENCE FOR EACH OF THE 8 DEP VARS (OWN PRICE ANNUAL CHANGE EXPECTED in 12M,, OBSERVED AT T+1, T+2, T+4; LAB DEMAND AT T+1, T+2, T+3, T+4)
* AFTER THAT RESULTS ARE USED TO GENERATE FIG AND TABLES

* ****************************************
* OWN EXPECTED PRICE IN 12M

quietly{
use WorkingData, clear

*** Here I use "IVQREGRESS SMOOTH" (KAPLAN SUN SIVQR)

sum dp
local ini=r(min)
local fin=r(max)
local nrows = $_fin - $_ini + 1

mat def SQTEP_B=J($_nrows,9,.)
mat def SQTEP_S=J($_nrows,9,.)


* setton5 has also setton5=5 since 2013q1 (constructions). 
* lack of setton5=5 in 2012q3 and 2012q4 screws up estimation of ivqregress smooth 
* hence for those qtrs I use different def of dummy sector
qui tab setton5, gen(SS)

drop if dprez==.

local r=1
forvalues t=$_ini/$_fin{
	local c = 1
	
	if $_t<=211{
	forvalues q=0.1(0.1)0.9{
		
		cap qui xi: ivqregress smooth  dprez dpre i.dim i.area SS2 SS3 SS4 (it12all=treatment) if dp==$_t, q($_q) 
		if _rc==0{
		mat SQTEP_B[$_r,$_c]=_b[it12all]
		mat SQTEP_S[$_r,$_c]=_se[it12all]
		}
		local c = $_c + 1
	}			
	}
	
	if $_t>=212{
	forvalues q=0.1(0.1)0.9{
		
		cap qui xi: ivqregress smooth  dprez dpre i.dim i.area i.setton5 (it12all=treatment) if dp==$_t, q($_q) 
		if _rc==0{
		mat SQTEP_B[$_r,$_c]=_b[it12all]
		mat SQTEP_S[$_r,$_c]=_se[it12all]
		}
		local c = $_c + 1
	}	
	}
	
	local r = $_r + 1
	
}


drop _all

mat def EB=SQTEP_B
mat def ES=SQTEP_S

svmat EB
svmat ES
gen dp=$_ini - 1 + _n
format dp %tq

label data "EP - From IVQREGRESS SMOOTH - reshaped long"
reshape long EB ES , i(dp) j(q)

save SQTE_EP_R500_IVQR, replace
}

sort dp q
* *************************************************************
* QUANTILE EFFECTS OF EXP INF ON OWN EXPECTED PRICES
* DP QUARTER; Q DECILE; EB COEFF ESTIMATE; ES STANDARD ERROR
list dp q EB ES
* *************************************************************

*** Now run IVQREGRESS ON ALL QUANTILES WHERE CONVERGENCE IS ACHIEVED TO TEST FOR COMMON EFFECT ETC...

* read results to create for each quarter a string of quantiles where convergence is achieved 
* hen run ivqregress jointly on such quantiles for each qtr, run tests of joint significance and store results

quietly{
local yvar="dprez"
local db="EP"
local xvar="EB"
local sevar="ES"

use SQTE_`db'_R500_IVQR, clear
sort dp q
by dp: gen i=_n
keep if $_xvar!=.  
/* here drops instances where ivqregress generates perfect fit (missing SE in reg output) as not enouhg info to conclude anything but erroneaously inflate KS test statistics*/
drop if $_sevar==0

keep dp q i
replace q=int(10*q)
gen s=string(q)
replace s=" " if s=="."
keep dp s i
reshape wide s, i(dp) j(i)

* S is a string var that now contains only the deciles at which IVQREGRESS has converged and estimated a coefficient
egen S=concat(s1 s2 s3 s4 s5 s6 s7 s8 s9), punct(" ")


* This transforms the S string for each quarter into a local variable Zqtr to be used below when running IVREGRESS jointly on all deciles where it is known to converge
local nobs=_N
forvalues j=1/$_nobs{
	local qtr=dp[$_j]
	local Z`qtr' = S[$_j]
}


use WorkingData, clear

sum dp
local ini=r(min)
local fin=r(max)
local nrows = $_fin - $_ini + 1

* Matrix now contains test stats, critical values and n of quantiles over which convergence is achieved
mat def SQTEP_B=J($_nrows,10,.)

qui tab setton5, gen(SS)

drop if $_yvar==.

local r=1
forvalues t=$_ini/$_fin{
	
	if $_t<=211{
	
		cap qui xi: ivqregress smooth $_yvar  dpre i.dim i.area SS2 SS3 SS4 (it12all=treatment) if dp==$_t, q("`Z`t''") 
		if _rc==0{
			qui estat endogeffect
			mat tt=r(table)
			mat SQTEP_B[$_r,1]=$_t
			mat SQTEP_B[$_r,2]=tt[1,1]
			mat SQTEP_B[$_r,3]=tt[1,2]
			mat SQTEP_B[$_r,4]=tt[2,1]
			mat SQTEP_B[$_r,5]=tt[2,2]
			mat SQTEP_B[$_r,6]=tt[3,1]
			mat SQTEP_B[$_r,7]=tt[3,2]
			mat SQTEP_B[$_r,8]=tt[4,1]
			mat SQTEP_B[$_r,9]=tt[4,2]			
			mat SQTEP_B[$_r,10]=e(n_q)	
			
		}

	}			
	
	
	if $_t>=212{
	
		
		cap qui xi: ivqregress smooth $_yvar  dpre i.dim i.area i.setton5 (it12all=treatment) if dp==$_t, q("`Z`t''") 
		if _rc==0{
			qui estat endogeffect
			mat tt=r(table)
			mat SQTEP_B[$_r,1]=$_t
			mat SQTEP_B[$_r,2]=tt[1,1]
			mat SQTEP_B[$_r,3]=tt[1,2]
			mat SQTEP_B[$_r,4]=tt[2,1]
			mat SQTEP_B[$_r,5]=tt[2,2]
			mat SQTEP_B[$_r,6]=tt[3,1]
			mat SQTEP_B[$_r,7]=tt[3,2]
			mat SQTEP_B[$_r,8]=tt[4,1]
			mat SQTEP_B[$_r,9]=tt[4,2]			
			mat SQTEP_B[$_r,10]=e(n_q)	
			
		}
	}	
	
	
	local r = $_r + 1
	
}

mat def Test_`xvar'_ = SQTEP_B
drop _all
svmat Test_`xvar'_
rename Test_`xvar'_1 dp
rename Test_`xvar'_2 NoEff
rename Test_`xvar'_3 CritNoEff
rename Test_`xvar'_4 ConstEff
rename Test_`xvar'_5 CritConstEff
rename Test_`xvar'_6 Dominance
rename Test_`xvar'_7 CritDominance
rename Test_`xvar'_8 Exog
rename Test_`xvar'_9 CritExog
rename Test_`xvar'_10 Nqs
gen var ="$_xvar"
format dp %tq
sort dp
save Test_`db', replace
}

* ========================================
* DEP VAR IS EXPECED PRICE CHANGE
* PRINT TO LOG TEST STATISTICS AND CRITICAL VALUES FOR
* H0:  No effect - the endogenous variables do not affect the outcome variable.
* H0: Constant effect - the effects of the endogenous variables do not vary across estimated quantiles.
* H0 rejected at 5% if Test Statistic > Critical Value

list var dp NoEff CritNoEff ConstEff CritConstEff 
* ========================================

* ****************************************
* OBSERVED ANNUAL PRICE CHANGE AT T+1 

quietly{

use WorkingData, clear

*** Here I use "IVQREGRESS SMOOTH" (KAPLAN - SUN SMOOTHED IVQR)


sum dp
local ini=r(min)
local fin=r(max)
local nrows = $_fin - $_ini + 1

drop if F1DPZ==.

sum dp
local ini=r(min)
local fin=r(max)


mat def SQTEP_B=J($_nrows,9,.)
mat def SQTEP_S=J($_nrows,9,.)


* setton5 has also setton5=5 since 2013q1 (constructions). 
* lack of setton5=5 in 2012q3 and 2012q4 screws up estimation of ivqregress smooth 
* hence for those qtrs I use different def of dummy sector
qui tab setton5, gen(SS)




local r=1
forvalues t=$_ini/$_fin{
	local c = 1
	
	if $_t<=211{
	forvalues q=0.1(0.1)0.9{
		
		cap qui xi: ivqregress smooth  F1DPZ dpre i.dim i.area SS2 SS3 SS4 (it12all=treatment) if dp==$_t, q($_q) 
		if _rc==0{
		mat SQTEP_B[$_r,$_c]=_b[it12all]
		mat SQTEP_S[$_r,$_c]=_se[it12all]
		}
		local c = $_c + 1
	}			
	}
	
	if $_t>=212{
	forvalues q=0.1(0.1)0.9{
		
		cap qui xi: ivqregress smooth  F1DPZ dpre i.dim i.area i.setton5 (it12all=treatment) if dp==$_t, q($_q) 
		if _rc==0{
		mat SQTEP_B[$_r,$_c]=_b[it12all]
		mat SQTEP_S[$_r,$_c]=_se[it12all]
		}
		local c = $_c + 1
	}	
	}
	
	local r = $_r + 1
	
}


drop _all

mat def P1B=SQTEP_B
mat def P1S=SQTEP_S

svmat P1B
svmat P1S
gen dp=$_ini - 1 + _n
format dp %tq

label data "P1 - From IVQREGRESS  - reshaped long"
reshape long P1B P1S , i(dp) j(q)

save SQTE_1P_R500_IVQR, replace
	
}

sort dp q
* *************************************************************
* QUANTILE EFFECTS OF EXP INF ON OBSERVED ANNUAL PRICE CHANGE AT T+1 
* DP QUARTER; Q DECILE; P1B COEFF ESTIMATE; P1S STANDARD ERROR
list dp q P1B P1S
* *************************************************************

*** Now run IVQREGRESS ON ALL QUANTILES WHERE CONVERGENCE IS ACHIEVED TO TEST FOR COMMON EFFECT ETC...

* read results to create for each quarter a string of quantiles where convergence is achieved 
* hen run ivqregress jointly on such quantiles for each qtr, run tests of joint significance and store results

quietly{
local yvar="F1DPZ"
local db="1P"
local xvar="P1B"
local sevar="P1S"


use SQTE_`db'_R500_IVQR, clear
sort dp q
by dp: gen i=_n
keep if $_xvar!=.
/* here drops instances where ivqregress generates perfect fit (missing SE in reg output) as not enouhg info to conclude anything but erroneaously inflate KS test statistics*/
drop if $_sevar==0

keep dp q i
replace q=int(10*q)
gen s=string(q)
replace s=" " if s=="."
keep dp s i
reshape wide s, i(dp) j(i)
egen S=concat(s1 s2 s3 s4 s5 s6 s7 s8 s9), punct(" ")


local nobs=_N
forvalues j=1/$_nobs{
	local qtr=dp[$_j]
	local Z`qtr' = S[$_j]
}


use WorkingData, clear



sum dp
local ini=r(min)
local fin=r(max)
local nrows = $_fin - $_ini + 1

* Matrix now contains test stats, critical values and n of quantiles over which convergence is achieved
mat def SQTEP_B=J($_nrows,10,.)



qui tab setton5, gen(SS)

drop if $_yvar==.

local r=1
forvalues t=$_ini/$_fin{

	if $_t<=211{
	
		cap qui xi: ivqregress smooth $_yvar  dpre i.dim i.area SS2 SS3 SS4 (it12all=treatment) if dp==$_t, q("`Z`t''") 
		if _rc==0{
			qui estat endogeffect
			mat tt=r(table)
			mat SQTEP_B[$_r,1]=$_t
			mat SQTEP_B[$_r,2]=tt[1,1]
			mat SQTEP_B[$_r,3]=tt[1,2]
			mat SQTEP_B[$_r,4]=tt[2,1]
			mat SQTEP_B[$_r,5]=tt[2,2]
			mat SQTEP_B[$_r,6]=tt[3,1]
			mat SQTEP_B[$_r,7]=tt[3,2]
			mat SQTEP_B[$_r,8]=tt[4,1]
			mat SQTEP_B[$_r,9]=tt[4,2]			
			mat SQTEP_B[$_r,10]=e(n_q)	
			
		}

	}			
	
	
	if $_t>=212{
	
		
		cap qui xi: ivqregress smooth $_yvar  dpre i.dim i.area i.setton5 (it12all=treatment) if dp==$_t, q("`Z`t''") 
		if _rc==0{
			qui estat endogeffect
			mat tt=r(table)
			mat SQTEP_B[$_r,1]=$_t
			mat SQTEP_B[$_r,2]=tt[1,1]
			mat SQTEP_B[$_r,3]=tt[1,2]
			mat SQTEP_B[$_r,4]=tt[2,1]
			mat SQTEP_B[$_r,5]=tt[2,2]
			mat SQTEP_B[$_r,6]=tt[3,1]
			mat SQTEP_B[$_r,7]=tt[3,2]
			mat SQTEP_B[$_r,8]=tt[4,1]
			mat SQTEP_B[$_r,9]=tt[4,2]			
			mat SQTEP_B[$_r,10]=e(n_q)	
			
		}
	}	
	
	
	local r = $_r + 1
	
}

mat def Test_`xvar'_ = SQTEP_B
drop _all
svmat Test_`xvar'_
rename Test_`xvar'_1 dp
rename Test_`xvar'_2 NoEff
rename Test_`xvar'_3 CritNoEff
rename Test_`xvar'_4 ConstEff
rename Test_`xvar'_5 CritConstEff
rename Test_`xvar'_6 Dominance
rename Test_`xvar'_7 CritDominance
rename Test_`xvar'_8 Exog
rename Test_`xvar'_9 CritExog
rename Test_`xvar'_10 Nqs
gen var ="$_xvar"
format dp %tq
save Test_`xvar', replace
}


* ========================================
* DEP VAR IS OBSERVED ANNUAL PRICE CHANGE AT T+1 
* PRINT TO LOG TEST STATISTICS AND CRITICAL VALUES FOR
* H0:  No effect - the endogenous variables do not affect the outcome variable.
* H0: Constant effect - the effects of the endogenous variables do not vary across estimated quantiles.
* H0 rejected at 5% if Test Statistic > Critical Value

list var dp NoEff CritNoEff ConstEff CritConstEff 
* ========================================

* ****************************************
* OBSERVED ANNUAL PRICE CHANGE AT T+2

quietly{

use WorkingData, clear

*** Here I use "IVQREGRESS SMOOTH" (KAPLAN - SUN SMOOTHED IVQR)


sum dp
local ini=r(min)
local fin=r(max)
local nrows = $_fin - $_ini + 1

drop if F2DPZ==.

sum dp
local ini=r(min)
local fin=r(max)


mat def SQTEP_B=J($_nrows,9,.)
mat def SQTEP_S=J($_nrows,9,.)


* setton5 has also setton5=5 since 2013q1 (constructions). 
* lack of setton5=5 in 2012q3 and 2012q4 screws up estimation of ivqregress smooth 
* hence for those qtrs I use different def of dummy sector
qui tab setton5, gen(SS)



local r=1
forvalues t=$_ini/$_fin{
	local c = 1
	
	if $_t<=211{
	forvalues q=0.1(0.1)0.9{
		
		cap qui xi: ivqregress smooth  F2DPZ dpre i.dim i.area SS2 SS3 SS4 (it12all=treatment) if dp==$_t, q($_q) 
		if _rc==0{
		mat SQTEP_B[$_r,$_c]=_b[it12all]
		mat SQTEP_S[$_r,$_c]=_se[it12all]
		}
		local c = $_c + 1
	}			
	}
	
	if $_t>=212{
	forvalues q=0.1(0.1)0.9{
		
		cap qui xi: ivqregress smooth  F2DPZ dpre i.dim i.area i.setton5 (it12all=treatment) if dp==$_t, q($_q) 
		if _rc==0{
		mat SQTEP_B[$_r,$_c]=_b[it12all]
		mat SQTEP_S[$_r,$_c]=_se[it12all]
		}
		local c = $_c + 1
	}	
	}
	
	local r = $_r + 1
	
}


drop _all

mat def P2B=SQTEP_B
mat def P2S=SQTEP_S

svmat P2B
svmat P2S
gen dp=$_ini - 1 + _n
format dp %tq

label data "P2 - From IVQREGRESS  - reshaped long"
reshape long P2B P2S , i(dp) j(q)

save SQTE_2P_R500_IVQR, replace

} 

sort dp q
* *************************************************************
* QUANTILE EFFECTS OF EXP INF ON OBSERVED ANNUAL PRICE CHANGE AT T+2
* DP QUARTER; Q DECILE; P2B COEFF ESTIMATE; P2S STANDARD ERROR
list dp q P2B P2S
* *************************************************************

*** Now run IVQREGRESS ON ALL QUANTILES WHERE CONVERGENCE IS ACHIEVED TO TEST FOR COMMON EFFECT ETC...

* read results to create for each quarter a string of quantiles where convergence is achieved 
* hen run ivqregress jointly on such quantiles for each qtr, run tests of joint significance and store results

quietly{

local yvar="F2DPZ"
local db="2P"
local xvar="P2B"
local sevar="P2S"


use SQTE_`db'_R500_IVQR, clear
sort dp q
by dp: gen i=_n
keep if $_xvar!=.
/* here drops instances where ivqregress generates perfect fit (missing SE in reg output) as not enouhg info to conclude anything but erroneaously inflate KS test statistics*/
drop if $_sevar==0

keep dp q i
replace q=int(10*q)
gen s=string(q)
replace s=" " if s=="."
keep dp s i
reshape wide s, i(dp) j(i)
egen S=concat(s1 s2 s3 s4 s5 s6 s7 s8 s9), punct(" ")


local nobs=_N
forvalues j=1/$_nobs{
	local qtr=dp[$_j]
	local Z`qtr' = S[$_j]
}


use WorkingData, clear



sum dp
local ini=r(min)
local fin=r(max)
local nrows = $_fin - $_ini + 1

* Matrix now contains test stats, critical values and n of quantiles over which convergence is achieved
mat def SQTEP_B=J($_nrows,10,.)



qui tab setton5, gen(SS)

drop if $_yvar==.

local r=1
forvalues t=$_ini/$_fin{


	if $_t<=211{
	
		cap qui xi: ivqregress smooth $_yvar  dpre i.dim i.area SS2 SS3 SS4 (it12all=treatment) if dp==$_t, q("`Z`t''") 
		if _rc==0{
			qui estat endogeffect
			mat tt=r(table)
			mat SQTEP_B[$_r,1]=$_t
			mat SQTEP_B[$_r,2]=tt[1,1]
			mat SQTEP_B[$_r,3]=tt[1,2]
			mat SQTEP_B[$_r,4]=tt[2,1]
			mat SQTEP_B[$_r,5]=tt[2,2]
			mat SQTEP_B[$_r,6]=tt[3,1]
			mat SQTEP_B[$_r,7]=tt[3,2]
			mat SQTEP_B[$_r,8]=tt[4,1]
			mat SQTEP_B[$_r,9]=tt[4,2]			
			mat SQTEP_B[$_r,10]=e(n_q)	
			
		}

	}			
	
	
	if $_t>=212{
	
		
		cap qui xi: ivqregress smooth $_yvar  dpre i.dim i.area i.setton5 (it12all=treatment) if dp==$_t, q("`Z`t''") 
		if _rc==0{
			qui estat endogeffect
			mat tt=r(table)
			mat SQTEP_B[$_r,1]=$_t
			mat SQTEP_B[$_r,2]=tt[1,1]
			mat SQTEP_B[$_r,3]=tt[1,2]
			mat SQTEP_B[$_r,4]=tt[2,1]
			mat SQTEP_B[$_r,5]=tt[2,2]
			mat SQTEP_B[$_r,6]=tt[3,1]
			mat SQTEP_B[$_r,7]=tt[3,2]
			mat SQTEP_B[$_r,8]=tt[4,1]
			mat SQTEP_B[$_r,9]=tt[4,2]			
			mat SQTEP_B[$_r,10]=e(n_q)	
			
		}
	}	
	
	
	local r = $_r + 1
	
}

mat def Test_`xvar'_ = SQTEP_B
drop _all
svmat Test_`xvar'_
rename Test_`xvar'_1 dp
rename Test_`xvar'_2 NoEff
rename Test_`xvar'_3 CritNoEff
rename Test_`xvar'_4 ConstEff
rename Test_`xvar'_5 CritConstEff
rename Test_`xvar'_6 Dominance
rename Test_`xvar'_7 CritDominance
rename Test_`xvar'_8 Exog
rename Test_`xvar'_9 CritExog
rename Test_`xvar'_10 Nqs
gen var ="$_xvar"
format dp %tq
save Test_`xvar', replace
	
}

* ========================================
* DEP VAR IS OBSERVED ANNUAL PRICE CHANGE AT T+2 
* PRINT TO LOG TEST STATISTICS AND CRITICAL VALUES FOR
* H0:  No effect - the endogenous variables do not affect the outcome variable.
* H0: Constant effect - the effects of the endogenous variables do not vary across estimated quantiles.
* H0 rejected at 5% if Test Statistic > Critical Value

list var dp NoEff CritNoEff ConstEff CritConstEff 
* ========================================

* ****************************************
* OBSERVED ANNUAL PRICE CHANGE AT T+4

quietly{
use WorkingData, clear

*** Here I use "IVQREGRESS SMOOTH" (KAPLAN - SUN SMOOTHED IVQR)


sum dp
local ini=r(min)
local fin=r(max)
local nrows = $_fin - $_ini + 1

drop if F4DPZ==.

sum dp
local ini=r(min)
local fin=r(max)


mat def SQTEP_B=J($_nrows,9,.)
mat def SQTEP_S=J($_nrows,9,.)


* setton5 has also setton5=5 since 2013q1 (constructions). 
* lack of setton5=5 in 2012q3 and 2012q4 screws up estimation of ivqregress smooth
* hence for those qtrs I use different def of dummy sector
qui tab setton5, gen(SS)



local r=1
forvalues t=$_ini/$_fin{
	local c = 1
	
	if $_t<=211{
	forvalues q=0.1(0.1)0.9{
		
		cap qui xi: ivqregress smooth F4DPZ dpre i.dim i.area SS2 SS3 SS4 (it12all=treatment) if dp==$_t, q($_q) 
		if _rc==0{
		mat SQTEP_B[$_r,$_c]=_b[it12all]
		mat SQTEP_S[$_r,$_c]=_se[it12all]
		}
		local c = $_c + 1
	}			
	}
	
	if $_t>=212{
	forvalues q=0.1(0.1)0.9{
		
		cap qui xi: ivqregress smooth F4DPZ dpre i.dim i.area i.setton5 (it12all=treatment) if dp==$_t, q($_q) 
		if _rc==0{
		mat SQTEP_B[$_r,$_c]=_b[it12all]
		mat SQTEP_S[$_r,$_c]=_se[it12all]
		}
		local c = $_c + 1
	}	
	}
	
	local r = $_r + 1
	
}


drop _all

mat def P4B=SQTEP_B
mat def P4S=SQTEP_S

svmat P4B
svmat P4S
gen dp=$_ini - 1 + _n
format dp %tq

label data "P4 - From IVQREGRESS - reshaped long"
reshape long P4B P4S , i(dp) j(q)

save SQTE_4P_R500_IVQR, replace

}

sort dp q
* *************************************************************
* QUANTILE EFFECTS OF EXP INF ON OBSERVED ANNUAL PRICE CHANGE AT T+4
* DP QUARTER; Q DECILE; P4B COEFF ESTIMATE; P4S STANDARD ERROR
list dp q P4B P4S
* *************************************************************

*** Now run IVQREGRESS ON ALL QUANTILES WHERE CONVERGENCE IS ACHIEVED TO TEST FOR COMMON EFFECT ETC...

* read results to create for each quarter a string of quantiles where convergence is achieved 
* hen run ivqregress jointly on such quantiles for each qtr, run tests of joint significance and store results

quietly{

local yvar="F4DPZ"
local db="4P"
local xvar="P4B"
local sevar="P4S"


use SQTE_`db'_R500_IVQR, clear
sort dp q
by dp: gen i=_n
keep if $_xvar!=.
/* here drops instances where ivqregress generates perfect fit (missing SE in reg output) as not enouhg info to conclude anything but erroneaously inflate KS test statistics*/
drop if $_sevar==0

keep dp q i
replace q=int(10*q)
gen s=string(q)
replace s=" " if s=="."
keep dp s i
reshape wide s, i(dp) j(i)
egen S=concat(s1 s2 s3 s4 s5 s6 s7 s8 s9), punct(" ")


local nobs=_N
forvalues j=1/$_nobs{
	local qtr=dp[$_j]
	local Z`qtr' = S[$_j]
}


use WorkingData, clear



sum dp
local ini=r(min)
local fin=r(max)
local nrows = $_fin - $_ini + 1

* Matrix now contains test stats, critical values and n of quantiles over which convergence is achieved
mat def SQTEP_B=J($_nrows,10,.)



qui tab setton5, gen(SS)

drop if $_yvar==.

local r=1
forvalues t=$_ini/$_fin{

	if $_t<=211{
	
		cap qui xi: ivqregress smooth $_yvar  dpre i.dim i.area SS2 SS3 SS4 (it12all=treatment) if dp==$_t, q("`Z`t''") 
		if _rc==0{
			qui estat endogeffect
			mat tt=r(table)
			mat SQTEP_B[$_r,1]=$_t
			mat SQTEP_B[$_r,2]=tt[1,1]
			mat SQTEP_B[$_r,3]=tt[1,2]
			mat SQTEP_B[$_r,4]=tt[2,1]
			mat SQTEP_B[$_r,5]=tt[2,2]
			mat SQTEP_B[$_r,6]=tt[3,1]
			mat SQTEP_B[$_r,7]=tt[3,2]
			mat SQTEP_B[$_r,8]=tt[4,1]
			mat SQTEP_B[$_r,9]=tt[4,2]			
			mat SQTEP_B[$_r,10]=e(n_q)	
			
		}

	}			
	
	
	if $_t>=212{
	
		
		cap qui xi: ivqregress smooth $_yvar  dpre i.dim i.area i.setton5 (it12all=treatment) if dp==$_t, q("`Z`t''") 
		if _rc==0{
			qui estat endogeffect
			mat tt=r(table)
			mat SQTEP_B[$_r,1]=$_t
			mat SQTEP_B[$_r,2]=tt[1,1]
			mat SQTEP_B[$_r,3]=tt[1,2]
			mat SQTEP_B[$_r,4]=tt[2,1]
			mat SQTEP_B[$_r,5]=tt[2,2]
			mat SQTEP_B[$_r,6]=tt[3,1]
			mat SQTEP_B[$_r,7]=tt[3,2]
			mat SQTEP_B[$_r,8]=tt[4,1]
			mat SQTEP_B[$_r,9]=tt[4,2]			
			mat SQTEP_B[$_r,10]=e(n_q)	
			
		}
	}	
	
	
	local r = $_r + 1
	
}

mat def Test_`xvar'_ = SQTEP_B
drop _all
svmat Test_`xvar'_
rename Test_`xvar'_1 dp
rename Test_`xvar'_2 NoEff
rename Test_`xvar'_3 CritNoEff
rename Test_`xvar'_4 ConstEff
rename Test_`xvar'_5 CritConstEff
rename Test_`xvar'_6 Dominance
rename Test_`xvar'_7 CritDominance
rename Test_`xvar'_8 Exog
rename Test_`xvar'_9 CritExog
rename Test_`xvar'_10 Nqs
gen var ="$_xvar"
format dp %tq
save Test_`xvar', replace

}

* ========================================
* DEP VAR IS OBSERVED ANNUAL PRICE CHANGE AT T+4 
* PRINT TO LOG TEST STATISTICS AND CRITICAL VALUES FOR
* H0:  No effect - the endogenous variables do not affect the outcome variable.
* H0: Constant effect - the effects of the endogenous variables do not vary across estimated quantiles.
* H0 rejected at 5% if Test Statistic > Critical Value

list var dp NoEff CritNoEff ConstEff CritConstEff 
* ========================================


* ****************************************
* OBSERVED LOG LABOR DEMAND AT T+1

quietly{
use WorkingData, clear


*** Here I use "IVQREGRESS SMOOTH" (KAPLAN - SUN SMOOTHED IVQR)


sum dp
local ini=r(min)
local fin=r(max)
local nrows = $_fin - $_ini + 1

keep if F1LAD!=.  /* otherwise ivqregress smooth screws up */

sum dp
local ini=r(min)
local fin=r(max)

mat def SQTEL_B=J($_nrows,9,.)
mat def SQTEL_S=J($_nrows,9,.)


qui tab setton5, gen(SS)


local r=1
forvalues t=$_ini/$_fin{
	local c = 1
	
	if $_t<=211{
	forvalues q=0.1(0.1)0.9{
		
		cap qui xi: ivqregress smooth F1LAD dpre i.dim i.area SS2 SS3 SS4 (it12all=treatment) if dp==$_t, q($_q)   
		if _rc==0{
		mat SQTEL_B[$_r,$_c]=_b[it12all]
		mat SQTEL_S[$_r,$_c]=_se[it12all]
		}
		local c = $_c + 1
		
	}
	}
	if $_t>=212{
	forvalues q=0.1(0.1)0.9{
		
		cap qui xi: ivqregress smooth F1LAD dpre i.dim i.area i.setton5 (it12all=treatment) if dp==$_t, q($_q)   
		if _rc==0{
		mat SQTEL_B[$_r,$_c]=_b[it12all]
		mat SQTEL_S[$_r,$_c]=_se[it12all]
		}
		local c = $_c + 1
		
	}
	}	
	
	local r = $_r + 1
	
}


drop _all

mat def L1B=SQTEL_B
mat def L1S=SQTEL_S

svmat L1B
svmat L1S
gen dp=$_ini - 1 + _n
format dp %tq

label data "L1- From IVQREGRESS  - reshaped long"
reshape long L1B L1S , i(dp) j(q)

save SQTE_L1_R500_IVQR, replace
	
}

sort dp q
* *************************************************************
* QUANTILE EFFECTS OF EXP INF ON LOG LABOR DEMAND AT T+1
* DP QUARTER; Q DECILE; L1B COEFF ESTIMATE; L1S STANDARD ERROR
list dp q L1B L1S
* *************************************************************

*** Now run IVQREGRESS ON ALL QUANTILES WHERE CONVERGENCE IS ACHIEVED TO TEST FOR COMMON EFFECT ETC...

* read results to create for each quarter a string of quantiles where convergence is achieved 
* hen run ivqregress jointly on such quantiles for each qtr, run tests of joint significance and store results

quietly{

local yvar="F1LAD"
local db="L1"
local xvar="L1B"
local sevar="L1S"


use SQTE_`db'_R500_IVQR, clear
sort dp q
by dp: gen i=_n
keep if $_xvar!=.
/* here drops instances where ivqregress generates perfect fit (missing SE in reg output) as not enouhg info to conclude anything but erroneaously inflate KS test statistics*/
drop if $_sevar==0

keep dp q i
replace q=int(10*q)
gen s=string(q)
replace s=" " if s=="."
keep dp s i
reshape wide s, i(dp) j(i)
egen S=concat(s1 s2 s3 s4 s5 s6 s7 s8 s9), punct(" ")


local nobs=_N
forvalues j=1/$_nobs{
	local qtr=dp[$_j]
	local Z`qtr' = S[$_j]
}


use WorkingData, clear



sum dp
local ini=r(min)
local fin=r(max)
local nrows = $_fin - $_ini + 1

* Matrix now contains test stats, critical values and n of quantiles over which convergence is achieved
mat def SQTEP_B=J($_nrows,10,.)



qui tab setton5, gen(SS)

drop if $_yvar==.

local r=1
forvalues t=$_ini/$_fin{

	if $_t<=211{
	
		cap qui xi: ivqregress smooth $_yvar  dpre i.dim i.area SS2 SS3 SS4 (it12all=treatment) if dp==$_t, q("`Z`t''") 
		if _rc==0{
			qui estat endogeffect
			mat tt=r(table)
			mat SQTEP_B[$_r,1]=$_t
			mat SQTEP_B[$_r,2]=tt[1,1]
			mat SQTEP_B[$_r,3]=tt[1,2]
			mat SQTEP_B[$_r,4]=tt[2,1]
			mat SQTEP_B[$_r,5]=tt[2,2]
			mat SQTEP_B[$_r,6]=tt[3,1]
			mat SQTEP_B[$_r,7]=tt[3,2]
			mat SQTEP_B[$_r,8]=tt[4,1]
			mat SQTEP_B[$_r,9]=tt[4,2]			
			mat SQTEP_B[$_r,10]=e(n_q)	
			
		}

	}			
	
	
	if $_t>=212{
	
		
		cap qui xi: ivqregress smooth $_yvar  dpre i.dim i.area i.setton5 (it12all=treatment) if dp==$_t, q("`Z`t''") 
		if _rc==0{
			qui estat endogeffect
			mat tt=r(table)
			mat SQTEP_B[$_r,1]=$_t
			mat SQTEP_B[$_r,2]=tt[1,1]
			mat SQTEP_B[$_r,3]=tt[1,2]
			mat SQTEP_B[$_r,4]=tt[2,1]
			mat SQTEP_B[$_r,5]=tt[2,2]
			mat SQTEP_B[$_r,6]=tt[3,1]
			mat SQTEP_B[$_r,7]=tt[3,2]
			mat SQTEP_B[$_r,8]=tt[4,1]
			mat SQTEP_B[$_r,9]=tt[4,2]			
			mat SQTEP_B[$_r,10]=e(n_q)	
			
		}
	}	
	
	
	local r = $_r + 1
	
}

mat def Test_`xvar'_ = SQTEP_B
drop _all
svmat Test_`xvar'_
rename Test_`xvar'_1 dp
rename Test_`xvar'_2 NoEff
rename Test_`xvar'_3 CritNoEff
rename Test_`xvar'_4 ConstEff
rename Test_`xvar'_5 CritConstEff
rename Test_`xvar'_6 Dominance
rename Test_`xvar'_7 CritDominance
rename Test_`xvar'_8 Exog
rename Test_`xvar'_9 CritExog
rename Test_`xvar'_10 Nqs
gen var ="$_xvar"
format dp %tq
save Test_`xvar', replace
	
}

* ========================================
* DEP VAR IS LOG LABOR DEMAND AT T+1
* PRINT TO LOG TEST STATISTICS AND CRITICAL VALUES FOR
* H0:  No effect - the endogenous variables do not affect the outcome variable.
* H0: Constant effect - the effects of the endogenous variables do not vary across estimated quantiles.
* H0 rejected at 5% if Test Statistic > Critical Value

list var dp NoEff CritNoEff ConstEff CritConstEff 
* ========================================


* ****************************************
* OBSERVED LOG LABOR DEMAND AT T+2

quietly{
use WorkingData, clear

*** Here I use "IVQREGRESS SMOOTH" (KAPLAN - SUN SMOOTHED IVQR)


sum dp
local ini=r(min)
local fin=r(max)
local nrows = $_fin - $_ini + 1

keep if F2LAD!=.  /* otherwise ivqregress smooth screws up */

sum dp
local ini=r(min)
local fin=r(max)


mat def SQTEL_B=J($_nrows,9,.)
mat def SQTEL_S=J($_nrows,9,.)


qui tab setton5, gen(SS)


local r=1
forvalues t=$_ini/$_fin{
	local c = 1
	
	if $_t<=211{
	forvalues q=0.1(0.1)0.9{
		
		cap qui xi: ivqregress smooth F2LAD dpre i.dim i.area SS2 SS3 SS4 (it12all=treatment) if dp==$_t, q($_q)   
		if _rc==0{
		mat SQTEL_B[$_r,$_c]=_b[it12all]
		mat SQTEL_S[$_r,$_c]=_se[it12all]
		}
		local c = $_c + 1
		
	}
	}
	if $_t>=212{
	forvalues q=0.1(0.1)0.9{
		
		cap qui xi: ivqregress smooth F2LAD dpre i.dim i.area i.setton5 (it12all=treatment) if dp==$_t, q($_q)   
		if _rc==0{
		mat SQTEL_B[$_r,$_c]=_b[it12all]
		mat SQTEL_S[$_r,$_c]=_se[it12all]
		}
		local c = $_c + 1
		
	}
	}	
	
	local r = $_r + 1
	
}


drop _all

mat def L2B=SQTEL_B
mat def L2S=SQTEL_S

svmat L2B
svmat L2S
gen dp=$_ini - 1 + _n
format dp %tq

label data "L2- From IVQREGRESS - reshaped long"
reshape long L2B L2S , i(dp) j(q)

save SQTE_L2_R500_IVQR, replace
	
}

sort dp q
* *************************************************************
* QUANTILE EFFECTS OF EXP INF ON LOG LABOR DEMAND AT T+2
* DP QUARTER; Q DECILE; L2B COEFF ESTIMATE; L2S STANDARD ERROR
list dp q L2B L2S
* *************************************************************

*** Now run IVQREGRESS ON ALL QUANTILES WHERE CONVERGENCE IS ACHIEVED TO TEST FOR COMMON EFFECT ETC...

* read results to create for each quarter a string of quantiles where convergence is achieved 
* hen run ivqregress jointly on such quantiles for each qtr, run tests of joint significance and store results

quietly{

local yvar="F2LAD"
local db="L2"
local xvar="L2B"
local sevar="L2S"


use SQTE_`db'_R500_IVQR, clear
sort dp q
by dp: gen i=_n
keep if $_xvar!=.
/* here drops instances where ivqregress generates perfect fit (missing SE in reg output) as not enouhg info to conclude anything but erroneaously inflate KS test statistics*/
drop if $_sevar==0

keep dp q i
replace q=int(10*q)
gen s=string(q)
replace s=" " if s=="."
keep dp s i
reshape wide s, i(dp) j(i)
egen S=concat(s1 s2 s3 s4 s5 s6 s7 s8 s9), punct(" ")


local nobs=_N
forvalues j=1/$_nobs{
	local qtr=dp[$_j]
	local Z`qtr' = S[$_j]
}


use WorkingData, clear



sum dp
local ini=r(min)
local fin=r(max)
local nrows = $_fin - $_ini + 1

* Matrix now contains test stats, critical values and n of quantiles over which convergence is achieved
mat def SQTEP_B=J($_nrows,10,.)



qui tab setton5, gen(SS)

drop if $_yvar==.

local r=1
forvalues t=$_ini/$_fin{

	if $_t<=211{
	
		cap qui xi: ivqregress smooth $_yvar  dpre i.dim i.area SS2 SS3 SS4 (it12all=treatment) if dp==$_t, q("`Z`t''") 
		if _rc==0{
			qui estat endogeffect
			mat tt=r(table)
			mat SQTEP_B[$_r,1]=$_t
			mat SQTEP_B[$_r,2]=tt[1,1]
			mat SQTEP_B[$_r,3]=tt[1,2]
			mat SQTEP_B[$_r,4]=tt[2,1]
			mat SQTEP_B[$_r,5]=tt[2,2]
			mat SQTEP_B[$_r,6]=tt[3,1]
			mat SQTEP_B[$_r,7]=tt[3,2]
			mat SQTEP_B[$_r,8]=tt[4,1]
			mat SQTEP_B[$_r,9]=tt[4,2]			
			mat SQTEP_B[$_r,10]=e(n_q)	
			
		}

	}			
	
	
	if $_t>=212{
	
		
		cap qui xi: ivqregress smooth $_yvar  dpre i.dim i.area i.setton5 (it12all=treatment) if dp==$_t, q("`Z`t''") 
		if _rc==0{
			qui estat endogeffect
			mat tt=r(table)
			mat SQTEP_B[$_r,1]=$_t
			mat SQTEP_B[$_r,2]=tt[1,1]
			mat SQTEP_B[$_r,3]=tt[1,2]
			mat SQTEP_B[$_r,4]=tt[2,1]
			mat SQTEP_B[$_r,5]=tt[2,2]
			mat SQTEP_B[$_r,6]=tt[3,1]
			mat SQTEP_B[$_r,7]=tt[3,2]
			mat SQTEP_B[$_r,8]=tt[4,1]
			mat SQTEP_B[$_r,9]=tt[4,2]			
			mat SQTEP_B[$_r,10]=e(n_q)	
			
		}
	}	
	
	
	local r = $_r + 1
	
}

mat def Test_`xvar'_ = SQTEP_B
drop _all
svmat Test_`xvar'_
rename Test_`xvar'_1 dp
rename Test_`xvar'_2 NoEff
rename Test_`xvar'_3 CritNoEff
rename Test_`xvar'_4 ConstEff
rename Test_`xvar'_5 CritConstEff
rename Test_`xvar'_6 Dominance
rename Test_`xvar'_7 CritDominance
rename Test_`xvar'_8 Exog
rename Test_`xvar'_9 CritExog
rename Test_`xvar'_10 Nqs
gen var ="$_xvar"
format dp %tq
save Test_`xvar', replace
	
}

* ========================================
* DEP VAR IS LOG LABOR DEMAND AT T+2
* PRINT TO LOG TEST STATISTICS AND CRITICAL VALUES FOR
* H0:  No effect - the endogenous variables do not affect the outcome variable.
* H0: Constant effect - the effects of the endogenous variables do not vary across estimated quantiles.
* H0 rejected at 5% if Test Statistic > Critical Value

list var dp NoEff CritNoEff ConstEff CritConstEff 
* ========================================


* ****************************************
* OBSERVED LOG LABOR DEMAND AT T+3

quietly{
use WorkingData, clear

*** Here I use "IVQREGRESS SMOOTH" (KAPLAN - SUN SMOOTHED IVQR)


sum dp
local ini=r(min)
local fin=r(max)
local nrows = $_fin - $_ini + 1

keep if F3LAD!=.  /* otherwise ivqregress smooth screws up */


sum dp
local ini=r(min)
local fin=r(max)




mat def SQTEL_B=J($_nrows,9,.)
mat def SQTEL_S=J($_nrows,9,.)


qui tab setton5, gen(SS)


local r=1
forvalues t=$_ini/$_fin{
	local c = 1
	
	if $_t<=211{
	forvalues q=0.1(0.1)0.9{
		
		cap qui xi: ivqregress smooth F3LAD dpre i.dim i.area SS2 SS3 SS4 (it12all=treatment) if dp==$_t, q($_q)   
		if _rc==0{
		mat SQTEL_B[$_r,$_c]=_b[it12all]
		mat SQTEL_S[$_r,$_c]=_se[it12all]
		}
		local c = $_c + 1
		
	}
	}
	if $_t>=212{
	forvalues q=0.1(0.1)0.9{
		
		cap qui xi: ivqregress smooth F3LAD dpre i.dim i.area i.setton5 (it12all=treatment) if dp==$_t, q($_q)   
		if _rc==0{
		mat SQTEL_B[$_r,$_c]=_b[it12all]
		mat SQTEL_S[$_r,$_c]=_se[it12all]
		}
		local c = $_c + 1
		
	}
	}	
	
	local r = $_r + 1
	
}


drop _all

mat def L3B=SQTEL_B
mat def L3S=SQTEL_S

svmat L3B
svmat L3S
gen dp=$_ini - 1 + _n
format dp %tq

label data "L3- From IVQREGRESS - reshaped long"
reshape long L3B L3S , i(dp) j(q)

save SQTE_L3_R500_IVQR, replace
	
}

sort dp q
* *************************************************************
* QUANTILE EFFECTS OF EXP INF ON LOG LABOR DEMAND AT T+3
* DP QUARTER; Q DECILE; L3B COEFF ESTIMATE; L3S STANDARD ERROR
list dp q L3B L3S
* *************************************************************

*** Now run IVQREGRESS ON ALL QUANTILES WHERE CONVERGENCE IS ACHIEVED TO TEST FOR COMMON EFFECT ETC...

* read results to create for each quarter a string of quantiles where convergence is achieved 
* hen run ivqregress jointly on such quantiles for each qtr, run tests of joint significance and store results

quietly{

local yvar="F3LAD"
local db="L3"
local xvar="L3B"
local sevar="L3S"



use SQTE_`db'_R500_IVQR, clear
sort dp q
by dp: gen i=_n
keep if $_xvar!=.
/* here drops instances where ivqregress generates perfect fit (missing SE in reg output) as not enouhg info to conclude anything but erroneaously inflate KS test statistics*/
drop if $_sevar==0

keep dp q i
replace q=int(10*q)
gen s=string(q)
replace s=" " if s=="."
keep dp s i
reshape wide s, i(dp) j(i)
egen S=concat(s1 s2 s3 s4 s5 s6 s7 s8 s9), punct(" ")


local nobs=_N
forvalues j=1/$_nobs{
	local qtr=dp[$_j]
	local Z`qtr' = S[$_j]
}


use WorkingData, clear



sum dp
local ini=r(min)
local fin=r(max)
local nrows = $_fin - $_ini + 1

* Matrix now contains test stats, critical values and n of quantiles over which convergence is achieved
mat def SQTEP_B=J($_nrows,10,.)



qui tab setton5, gen(SS)

drop if $_yvar==.

local r=1
forvalues t=$_ini/$_fin{

	if $_t<=211{
	
		cap qui xi: ivqregress smooth $_yvar  dpre i.dim i.area SS2 SS3 SS4 (it12all=treatment) if dp==$_t, q("`Z`t''") 
		if _rc==0{
			qui estat endogeffect
			mat tt=r(table)
			mat SQTEP_B[$_r,1]=$_t
			mat SQTEP_B[$_r,2]=tt[1,1]
			mat SQTEP_B[$_r,3]=tt[1,2]
			mat SQTEP_B[$_r,4]=tt[2,1]
			mat SQTEP_B[$_r,5]=tt[2,2]
			mat SQTEP_B[$_r,6]=tt[3,1]
			mat SQTEP_B[$_r,7]=tt[3,2]
			mat SQTEP_B[$_r,8]=tt[4,1]
			mat SQTEP_B[$_r,9]=tt[4,2]			
			mat SQTEP_B[$_r,10]=e(n_q)	
			
		}

	}			
	
	
	if $_t>=212{
	
		
		cap qui xi: ivqregress smooth $_yvar  dpre i.dim i.area i.setton5 (it12all=treatment) if dp==$_t, q("`Z`t''") 
		if _rc==0{
			qui estat endogeffect
			mat tt=r(table)
			mat SQTEP_B[$_r,1]=$_t
			mat SQTEP_B[$_r,2]=tt[1,1]
			mat SQTEP_B[$_r,3]=tt[1,2]
			mat SQTEP_B[$_r,4]=tt[2,1]
			mat SQTEP_B[$_r,5]=tt[2,2]
			mat SQTEP_B[$_r,6]=tt[3,1]
			mat SQTEP_B[$_r,7]=tt[3,2]
			mat SQTEP_B[$_r,8]=tt[4,1]
			mat SQTEP_B[$_r,9]=tt[4,2]			
			mat SQTEP_B[$_r,10]=e(n_q)	
			
		}
	}	
	
	
	local r = $_r + 1
	
}

mat def Test_`xvar'_ = SQTEP_B
drop _all
svmat Test_`xvar'_
rename Test_`xvar'_1 dp
rename Test_`xvar'_2 NoEff
rename Test_`xvar'_3 CritNoEff
rename Test_`xvar'_4 ConstEff
rename Test_`xvar'_5 CritConstEff
rename Test_`xvar'_6 Dominance
rename Test_`xvar'_7 CritDominance
rename Test_`xvar'_8 Exog
rename Test_`xvar'_9 CritExog
rename Test_`xvar'_10 Nqs
gen var ="$_xvar"
format dp %tq
save Test_`xvar', replace
	
}

* ========================================
* DEP VAR IS LOG LABOR DEMAND AT T+3
* PRINT TO LOG TEST STATISTICS AND CRITICAL VALUES FOR
* H0:  No effect - the endogenous variables do not affect the outcome variable.
* H0: Constant effect - the effects of the endogenous variables do not vary across estimated quantiles.
* H0 rejected at 5% if Test Statistic > Critical Value

list var dp NoEff CritNoEff ConstEff CritConstEff 
* ========================================


* ****************************************
* OBSERVED LOG LABOR DEMAND AT T+4

quietly{
use WorkingData, clear

*** Here I use "IVQREGRESS SMOOTH" (KAPLAN - SUN SMOOTHED IVQR)


sum dp
local ini=r(min)
local fin=r(max)
local nrows = $_fin - $_ini + 1

keep if F4LAD!=.  /* otherwise ivqregress smooth  screws up */

sum dp
local ini=r(min)
local fin=r(max)



mat def SQTEL_B=J($_nrows,9,.)
mat def SQTEL_S=J($_nrows,9,.)


qui tab setton5, gen(SS)


local r=1
forvalues t=$_ini/$_fin{
	local c = 1
	
	if $_t<=211{
	forvalues q=0.1(0.1)0.9{
		
		cap qui xi: ivqregress smooth  F4LAD dpre i.dim i.area SS2 SS3 SS4 (it12all=treatment) if dp==$_t, q($_q)   
		if _rc==0{
		mat SQTEL_B[$_r,$_c]=_b[it12all]
		mat SQTEL_S[$_r,$_c]=_se[it12all]
		}
		local c = $_c + 1
		
	}
	}
	if $_t>=212{
	forvalues q=0.1(0.1)0.9{
		
		cap qui xi: ivqregress smooth  F4LAD dpre i.dim i.area i.setton5 (it12all=treatment) if dp==$_t, q($_q)   
		if _rc==0{
		mat SQTEL_B[$_r,$_c]=_b[it12all]
		mat SQTEL_S[$_r,$_c]=_se[it12all]
		}
		local c = $_c + 1
		
	}
	}	
	
	local r = $_r + 1
	
}


drop _all

mat def L4B=SQTEL_B
mat def L4S=SQTEL_S

svmat L4B
svmat L4S
gen dp=$_ini - 1 + _n
format dp %tq

label data "L4- From IVQREGRESS  - reshaped long"
reshape long L4B L4S , i(dp) j(q)

save SQTE_L4_R500_IVQR, replace

}

sort dp q
* *************************************************************
* QUANTILE EFFECTS OF EXP INF ON LOG LABOR DEMAND AT T+4
* DP QUARTER; Q DECILE; L4B COEFF ESTIMATE; L4S STANDARD ERROR
list dp q L4B L4S
* *************************************************************

*** Now run IVQREGRESS ON ALL QUANTILES WHERE CONVERGENCE IS ACHIEVED TO TEST FOR COMMON EFFECT ETC...

* read results to create for each quarter a string of quantiles where convergence is achieved 
* hen run ivqregress jointly on such quantiles for each qtr, run tests of joint significance and store results

quietly{

local yvar="F4LAD"
local db="L4"
local xvar="L4B"
local sevar="L4S"



use SQTE_`db'_R500_IVQR, clear
sort dp q
by dp: gen i=_n
keep if $_xvar!=.
/* here drops instances where ivqregress generates perfect fit (missing SE in reg output) as not enouhg info to conclude anything but erroneaously inflate KS test statistics*/
drop if $_sevar==0

keep dp q i
replace q=int(10*q)
gen s=string(q)
replace s=" " if s=="."
keep dp s i
reshape wide s, i(dp) j(i)
egen S=concat(s1 s2 s3 s4 s5 s6 s7 s8 s9), punct(" ")


local nobs=_N
forvalues j=1/$_nobs{
	local qtr=dp[$_j]
	local Z`qtr' = S[$_j]
}


use WorkingData, clear



sum dp
local ini=r(min)
local fin=r(max)
local nrows = $_fin - $_ini + 1

* Matrix now contains test stats, critical values and n of quantiles over which convergence is achieved
mat def SQTEP_B=J($_nrows,10,.)



qui tab setton5, gen(SS)

drop if $_yvar==.

local r=1
forvalues t=$_ini/$_fin{


	if $_t<=211{
	
		cap qui xi: ivqregress smooth $_yvar  dpre i.dim i.area SS2 SS3 SS4 (it12all=treatment) if dp==$_t, q("`Z`t''") 
		if _rc==0{
			qui estat endogeffect
			mat tt=r(table)
			mat SQTEP_B[$_r,1]=$_t
			mat SQTEP_B[$_r,2]=tt[1,1]
			mat SQTEP_B[$_r,3]=tt[1,2]
			mat SQTEP_B[$_r,4]=tt[2,1]
			mat SQTEP_B[$_r,5]=tt[2,2]
			mat SQTEP_B[$_r,6]=tt[3,1]
			mat SQTEP_B[$_r,7]=tt[3,2]
			mat SQTEP_B[$_r,8]=tt[4,1]
			mat SQTEP_B[$_r,9]=tt[4,2]			
			mat SQTEP_B[$_r,10]=e(n_q)	
			
		}

	}			
	
	
	if $_t>=212{
	
		
		cap qui xi: ivqregress smooth $_yvar  dpre i.dim i.area i.setton5 (it12all=treatment) if dp==$_t, q("`Z`t''") 
		if _rc==0{
			qui estat endogeffect
			mat tt=r(table)
			mat SQTEP_B[$_r,1]=$_t
			mat SQTEP_B[$_r,2]=tt[1,1]
			mat SQTEP_B[$_r,3]=tt[1,2]
			mat SQTEP_B[$_r,4]=tt[2,1]
			mat SQTEP_B[$_r,5]=tt[2,2]
			mat SQTEP_B[$_r,6]=tt[3,1]
			mat SQTEP_B[$_r,7]=tt[3,2]
			mat SQTEP_B[$_r,8]=tt[4,1]
			mat SQTEP_B[$_r,9]=tt[4,2]			
			mat SQTEP_B[$_r,10]=e(n_q)	
			
		}
	}	
	
	
	local r = $_r + 1
	
}

mat def Test_`xvar'_ = SQTEP_B
drop _all
svmat Test_`xvar'_
rename Test_`xvar'_1 dp
rename Test_`xvar'_2 NoEff
rename Test_`xvar'_3 CritNoEff
rename Test_`xvar'_4 ConstEff
rename Test_`xvar'_5 CritConstEff
rename Test_`xvar'_6 Dominance
rename Test_`xvar'_7 CritDominance
rename Test_`xvar'_8 Exog
rename Test_`xvar'_9 CritExog
rename Test_`xvar'_10 Nqs
gen var ="$_xvar"
format dp %tq
save Test_`xvar', replace

}

* ========================================
* DEP VAR IS LOG LABOR DEMAND AT T+4
* PRINT TO LOG TEST STATISTICS AND CRITICAL VALUES FOR
* H0:  No effect - the endogenous variables do not affect the outcome variable.
* H0: Constant effect - the effects of the endogenous variables do not vary across estimated quantiles.
* H0 rejected at 5% if Test Statistic > Critical Value

list var dp NoEff CritNoEff ConstEff CritConstEff 
* ========================================


* ********************* GENERATE FIG 8 *********************************************

* FIG 8 T STAT OF COEFFS OF IVQREGRESS

* USE RESULTS FROM IVQREGRESS ESTMATIONS STORED IN SQTE_XX_R500_IVQR.dta T COMPUTE TSTATS
* THEN CREATE BINS B OF WIDTH 0.2 AND COMPUTE SHARE OF TSTATS S IN EACH BIN BY DEPVAR (V) HORIZON (H)
* PLOT SHARE S AGAINST BIN B BY DEPVAR (FIG 8)
quietly{
use SQTE_EP_R500_IVQR, clear
merge 1:1 dp q using SQTE_1P_R500_IVQR
drop _m
merge 1:1 dp q using SQTE_2P_R500_IVQR
drop _m
merge 1:1 dp q using SQTE_4P_R500_IVQR
drop _m
merge 1:1 dp q using SQTE_L1_R500_IVQR 
drop _m
merge 1:1 dp q using SQTE_L2_R500_IVQR
drop _m
merge 1:1 dp q using SQTE_L3_R500_IVQR
drop _m
merge 1:1 dp q using SQTE_L4_R500_IVQR
drop _m

local i = 1
foreach x in E P1 P2 P4{
	gen TP$_i = `x'B/`x'S
	local i = $_i + 1
}

foreach x in L1 L2 L3 L4{
	gen T$_x = `x'B/`x'S
}


keep T* dp q
reshape long TP TL , i (dp q) j(h)


reshape long T  , i (dp q h) j(v, string)

* drop triplets dp-h-v for which sample size is zero because out of sample (e-g- demand 4 qtr ahead in 2022:q1)
drop if v=="L" & h==4 & dp>=yq(2022,1)  /* lab dem 4 qtrs ahead */
drop if v=="L" & h==3 & dp>=yq(2022,2)  /* lab dem 3 qtrs ahead */
drop if v=="L" & h==2 & dp>=yq(2022,3)  /* lab dem 2 atrs ahead */
drop if v=="L" & h==1 & dp>=yq(2022,4)  /* lab dem 1 atr  ahead */

drop if v=="P" & h==4 & dp>=yq(2022,1)  /* price 4 qtrs ahead */
drop if v=="P" & h==3 & dp>=yq(2022,3)  /* price 2 qtrs ahead */
drop if v=="P" & h==2 & dp>=yq(2022,4)  /* price 1 qtr  ahead */

count if T==.
count if T!=. /* these are quantiles where convergence non achieved or model fits perfectly (SE=0) */

drop if T==.
* ****

gen b=.
replace b=1 if T<-3.5

local i = 2
forvalues j=-3.5(0.2)3.5{
replace b=$_i if T>=$_j & T<`=$_j+0.2'
local i =$_i + 1
}
di $_i
replace b=$_i if T>=3.5 & T!=.

* number of TSTATS IN BIN b FOR VAR v HORIZON h 
egen Z=count(T), by(v h b )
sort v h b
* select only one datapoint by bin/var/hz 
by v h b : gen x=_n==1
gen Z1=Z*x
egen TT=sum(Z1), by(v h)
table v, stat(sum Z1)

gen S=100*Z/TT


egen G=group(v h)

cap lab drop lg
lab def lg 1 "observed at t+1" 2 "observed at t+2" 3 "observed at t+3" 4 "observed at t+4" 5 "expected 1 year at t" 6 "observed at t+1" 7 "observed at t+2" 8 "observed at t+4"
lab val G lg
}

gen z = -3.6 + 0.2 * (b-1)
format z %3.1f
twoway bar S z if x==1, barw(0.2) by(G, note("") graphregion(c(white)) row(2)) subti(,bc(white)) xla(-3.5(0.5)3.5, valuelab angle(270) labsi(*0.7)) xsize(10) ysize(6) yti("") xti("t-statistic") yla(,angle(0) labsi(*0.7)) xli(-1.96 +1.96)

gr_edit .plotregion1.r2title[4]._set_orientation rvertical
gr_edit .plotregion1.r2title[4].text = {}
gr_edit .plotregion1.r2title[4].text.Arrpush Labor demand
gr_edit .plotregion1.r2title[8].text = {}
gr_edit .plotregion1.r2title[8].text.Arrpush Annual price change


graph export Fig8.eps, replace 

* ===========================================================
* PRINT TO LOG SHARES S AND BINS UNDERLYING FIG 8
* see above for ow to construct figure
sort v h b
list v h G S z b if x==1
* ===========================================================

* ********************* GENERATE FIG 8 : END *********************************************

* ********************* GENERATE TAB H1-H2 ************************************************

* TABLE H1  - FOR EACH QTR LIST QUANTILES WITH 5% STAT SIGN EFFECT OF EXP INLF ON OWN PRICE 

quietly{
use SQTE_EP_R500_IVQR, clear
merge 1:1 dp q using SQTE_1P_R500_IVQR
drop _m
merge 1:1 dp q using SQTE_2P_R500_IVQR
drop _m
merge 1:1 dp q using SQTE_4P_R500_IVQR
drop _m



foreach x in E P1 P2 P4{
	gen t$_x = `x'B/`x'S
}

keep t* dp q
reshape long t , i (dp q) j(v, string)

gen sig5 =(abs(t)>1.96)
replace sig5=. if t==.
preserve
collapse (sum) sig5 (max) t, by(dp v)
replace sig5 = . if t==.
drop t
reshape wide sig5, i(dp) j(v, string)
mkmat *, mat(P)
restore

replace sig5=. if sig5==0
gen sigq=sig5*q
drop sig5 t
reshape wide sigq, i(dp v) j(q)
egen qsig=concat(sigq1 sigq2 sigq3 sigq4 sigq5 sigq6 sigq7 sigq8 sigq9), punct(";")
keep dp v qsig
gen z=regexr(qsig,"\.;|;\.","")
forvalues j=1/9{
gen tmp=regexr(z,"\.;|;\.","")
drop z
rename tmp z
}
gen SigQ=regexr(z,"\.","")
keep dp v SigQ
reshape wide SigQ, i(dp) j(v, string)
gen cc="\\"

* ***Table H1 - own prices
outsheet dp SigQE SigQP1 SigQP2 SigQP4 cc   using TabWhatSigQ_IVQR_P.out, delim("&") replace  


}

* ==============================================
* PRINT TO LOG RESULTS
* SigQE  = Quantiles at which effect on own expected price is 5% stat sign
* SigQPX = Quantiles at which effect on own price at T+X is 5% stat sign
format dp %tq
* *************************************
* *************** TAB H1 **************
* *************************************
list dp SigQE SigQP1 SigQP2 SigQP4 , linesize(255) header noobs
* ==============================================

* TABLE H2  - FOR EACH QTR LIST QUANTILES WITH 5% STAT SIGN EFFECT OF EXP INLF ON LAB DEMAND

quietly{
use SQTE_L1_R500_IVQR, clear
merge 1:1 dp q using SQTE_L2_R500_IVQR
drop _m
merge 1:1 dp q using SQTE_L3_R500_IVQR
drop _m
merge 1:1 dp q using SQTE_L4_R500_IVQR
drop _m



foreach x in L1 L2 L3 L4{
	gen t$_x = `x'B/`x'S
}

keep t* dp q
reshape long t , i (dp q) j(v, string)

gen sig5 =(abs(t)>1.96)
replace sig5=. if t==.
preserve
collapse (sum) sig5 (max) t, by(dp v)
replace sig5 = . if t==.
drop t
reshape wide sig5, i(dp) j(v, string)
mkmat *, mat(P)
restore

replace sig5=. if sig5==0
gen sigq=sig5*q
drop sig5 t
reshape wide sigq, i(dp v) j(q)
egen qsig=concat(sigq1 sigq2 sigq3 sigq4 sigq5 sigq6 sigq7 sigq8 sigq9), punct(";")
keep dp v qsig
gen z=regexr(qsig,"\.;|;\.","")
forvalues j=1/9{
gen tmp=regexr(z,"\.;|;\.","")
drop z
rename tmp z
}
gen SigQ=regexr(z,"\.","")
keep dp v SigQ
reshape wide SigQ, i(dp) j(v, string)
gen cc="\\"

* Table H2 - Labor demand
outsheet dp SigQL1 SigQL2 SigQL3 SigQL4 cc  using TabWhatSigQ_IVQR_L.out, delim("&") replace  

}


* ==============================================
* PRINT TO LOG RESULTS
* SigQLX = Quantiles at which effect on labor demand at T+X is 5% stat sign
format dp %tq
* *************************************
* *************** TAB H2 **************
* *************************************
list dp SigQL1 SigQL2 SigQL3 SigQL4, linesize(255) header noobs
* ==============================================
* ********************* GENERATE TAB H1-H2: END ************************************************


* ********************* GENERATE TAB H3-H4 **************************************************

* ************************************
* TABLES H3-H4 report test results of joint IVQR estimation
* 1 table for each set of P and L variables.
* for each depvar two cols, one for each null H0 of no effect along distr and H0 of constant effect along distr
* table item is (R) if H0 rejected at 5% and empty otherwise

* TABLE H3  - OWN PRICES
quietly{
use Test_EP, clear
append using Test_P1B
append using Test_P2B
append using Test_P4B

drop if dp==. /* wnhen no obs available for ivqreg estimation */

gen RNoE="R" if NoEff>CritNoEff
gen RCnE="R" if ConstEff>CritConstEff

egen C=group(var)
keep dp RNo RCn C

reshape wide RNo RCn, i(dp) j(C)

format dp %tq
gen z1=""
gen z2=""
gen z3=""
gen z4="\\"

* Table H3 - prices
order dp RNoE1 RCnE1 z1 RNoE2 RCnE2 z2 RNoE3 RCnE3 z3 RNoE4 RCnE4 z4 
outsheet * using TabTestIVQR_P, delim("&") replace
}
* ==========================================
* PRINT TABLE H3 TO LOG
* RNoEX = R if h0 of no effect on EX is rejected at 5%, empty otherwhise
* RCnEX = R if h0 of constatn effect on EX is rejected at 5%, empty otherwhise
* EX is own price, X=1 expected, X=2 observed at T+1, X=3 observed at T+2, X=4 observed at T+4
format dp %tq
* *************************************
* *************** TAB H3 **************
* *************************************
list dp RNoE1 RCnE1 RNoE2 RCnE2 RNoE3 RCnE3 RNoE4 RCnE4 , linesize(255) header noobs
* ==========================================

* TABLE H4 - LABOR DEMAND
quietly{
use Test_L1B, clear
append using Test_L2B
append using Test_L3B
append using Test_L4B

drop if dp==. 

gen RNoE="R" if NoEff>CritNoEff
gen RCnE="R" if ConstEff>CritConstEff

egen C=group(var)
keep dp RNo RCn C

reshape wide RNo RCn, i(dp) j(C)

format dp %tq
gen z1=""
gen z2=""
gen z3=""
gen z4="\\"

* Table H4 - labor demand
order dp RNoE1 RCnE1 z1 RNoE2 RCnE2 z2 RNoE3 RCnE3 z3 RNoE4 RCnE4 z4 
outsheet * using TabTestIVQR_L, delim("&") replace

}

* ==========================================
* PRINT TABLE H4 TO LOG
* RNoEX = R if h0 of no effect on EX is rejected at 5%, empty otherwhise
* RCnEX = R if h0 of constatn effect on EX is rejected at 5%, empty otherwhise
* EX is lab demand , X=1 observed at T+1, X=2 observed at T+2, X=3 oberved at T+3, X=4 observed at T+4
format dp %tq
* *************************************
* *************** TAB H4 **************
* *************************************
list dp RNoE1 RCnE1 RNoE2 RCnE2 RNoE3 RCnE3 RNoE4 RCnE4 , linesize(255) header noobs
* ==========================================
